package com.wuyi.service.impl;

import com.wuyi.mapper.CommentMapper;
import com.wuyi.pojo.Comment;
import com.wuyi.pojo.bo.CommentBO;
import com.wuyi.service.CommentService;
import com.zc.utils.JSONResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName CommentServiceImpl.java
 * @Description 评论实现类
 * @createTime 2021年11月04日 00:01:00
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;



    /**
     * 新增评论
     *
     * @param commentBO
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult addComment(CommentBO commentBO) {

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentBO, comment);
        comment.setCommentTime(new Date());
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());

        int res = commentMapper.insert(comment);
        if (res != 1) {
            return JSONResult.errorMsg("发表评论失败");
        }
        return JSONResult.ok("发表评论成功");
    }

    /**
     * 删除评论
     *
     * @param commentId
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult deleteComment(Integer commentId) {
        int res = commentMapper.deleteByPrimaryKey(commentId);
        if (res != 1) {
            return JSONResult.errorMsg("删除失败");
        }
        return JSONResult.ok("删除成功");

    }
}
